package com.estar.appuser.util;

import java.security.Key;
import javax.crypto.Cipher;
import javax.crypto.SecretKeyFactory;
import javax.crypto.spec.DESedeKeySpec;
import javax.crypto.spec.IvParameterSpec;

import jodd.util.Base64;

/** 
 * 3DES加密工具类 
 */  
public class Des3 {  
    // 密钥  
    private final static String secretKey = "estar2828rfwe$101$@tgf**";  
    // 向量  
    private final static String iv = "31875920";  
    // 加解密统一使用的编码方式  
    private final static String encoding = "UTF-8";  
  
    /** 
     * 3DES加密 
     *  
     * @param plainText 普通文本 
     * @return 
     * @throws Exception  
     */  
    public static String encode(String plainText) throws Exception {  
        Key deskey = null;  
        DESedeKeySpec spec = new DESedeKeySpec(secretKey.getBytes());  
        SecretKeyFactory keyfactory = SecretKeyFactory.getInstance("desede");  
        deskey = keyfactory.generateSecret(spec);  
        Cipher cipher = Cipher.getInstance("desede/CBC/PKCS5Padding");  
        IvParameterSpec ips = new IvParameterSpec(iv.getBytes());  
        cipher.init(Cipher.ENCRYPT_MODE, deskey, ips);  
        byte[] encryptData = cipher.doFinal(plainText.getBytes(encoding));  
        return new String(jodd.util.Base64.encodeToByte(encryptData), encoding) ;  
    } 
   
    /** 
     * 3DES解密 
     * @param encryptText 加密文本 
     * @return 
     * @throws Exception 
     */  
    public static String decode(String encryptText, String encoding) throws Exception {  
    	if(encryptText==null || encryptText.equals("")){
    		return "";
    	}
    	encryptText = encryptText.replace(" ", "+");
    	System.out.println("encryptText-------"+encryptText);
    	
        Key deskey = null;  
        DESedeKeySpec spec = new DESedeKeySpec(secretKey.getBytes());  
        SecretKeyFactory keyfactory = SecretKeyFactory.getInstance("desede");  
        deskey = keyfactory.generateSecret(spec);  
        Cipher cipher = Cipher.getInstance("desede/CBC/PKCS5Padding");  
        IvParameterSpec ips = new IvParameterSpec(iv.getBytes());  
        cipher.init(Cipher.DECRYPT_MODE, deskey, ips);  
  
        byte[] decryptData = cipher.doFinal(Base64.decode(encryptText));  
  
        return new String(decryptData, encoding);  
    }
  
    /** 
     * 3DES解密 
     *  
     * @param encryptText 加密文本 
     * @return 
     * @throws Exception 
     */  
    public static String decode(String encryptText) throws Exception {  
        return decode(encryptText, encoding);  
    }  
    
    public static void main(String[] args) {
    	  
    	  
    	  String content = "12345678";  
    	  try
    	  {
    	  //加密
    	  System.out.println("加密前：" + content);
    	  String encryptResult = encode(content);
    	  System.out.println("加密：" + encryptResult);
    	  String enc = decode("7eZWrCbVwt7TVpEM32QNvA==");
    	  System.out.println("解密：" + enc);
    	  }catch(Exception ex)
    	  {
    		  ex.printStackTrace();
    	  }

    	 }
 }
